using UnityEngine;
using System.Collections;
using DanielSig.UnityExtensions;

public class HumanAgent : MonoBehaviour, IAgent
{
	[Range(-1,1)]
	public float MoveForwardThreshold = 0.7F;
	
	public Move NextMove (ISensor[] input)
	{
		Move move = Move.None;
		
		Vector2 movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
		movement = movement.Rotate(-Camera.main.transform.eulerAngles.y);
		
		if(movement != Vector2.zero)
		{
			Vector2 rightSide = transform.right.XZ();
			Vector2 forward = transform.forward.XZ();
			
			if(Vector2.Dot(movement, rightSide) > 0)
				move |= Move.TurnRight;
			else
				move |= Move.TurnLeft;
			
			if(Vector2.Dot(movement, forward) > MoveForwardThreshold)
				move |= Move.GoForward;
		}
		else
		{
			float turn = Input.GetAxis("Turning");
			float moveAmount = Input.GetAxis("Movement");
			
			if(moveAmount > 0)
				move |= Move.GoForward;
			if(turn < 0)
				move |= Move.TurnLeft;
			if(turn > 0)
				move |= Move.TurnRight;
		}
		if(Input.GetButton("Fire") || (Input.GetMouseButton(0)))
			move |= Move.Shoot;
		
		Screen.lockCursor = IsFirstPerson;
		
		return move;
	}
	private bool IsFirstPerson
	{
		get{ return !Camera.main.isOrthoGraphic; }
	}
}

